package com.firexxx.vp.domain.vp.single.mlvdata;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: TODO
 * @author: zhuangzhang2
 * @date: 2020-12-30 11:22
 */
public class MlvModelData {
    private MlvTotalHead totalHead;
    private int[] ivecTypeUse;
    private MlvModelItem plp_tv;
    private MlvModelItem plp_dnn_tv;
    private MlvModelItem pncc_tv;
    private MlvModelItem pncc_dnn_tv;
    private MlvModelItem cnn;

    public MlvModelData(byte[] modelBuffer) throws Exception {
        this(modelBuffer, true);
    }

    public MlvModelData(byte[] modelBuffer, boolean isNeedFloat) throws Exception {
        this.totalHead = new MlvTotalHead();
        this.ivecTypeUse = new int[5];
        Map<MlvIvecTypeItem, MlvModelItem> itemMap = new HashMap(5);
        InputStream in = new ByteArrayInputStream(modelBuffer);
        Throwable var5 = null;

        try {
            DataInputStream dataInputStream = new DataInputStream(in);
            Throwable var7 = null;

            try {
                short[] values4Short = new short[3];
                byte[] bytes4Short = new byte[2];

                for(int i = 0; i < values4Short.length; ++i) {
                    dataInputStream.readFully(bytes4Short);
                    values4Short[i] = getShort(bytes4Short);
                }

                this.totalHead.setNum_ivec(values4Short[0]);
                this.totalHead.setLen_ivec_data(values4Short[1]);
                this.totalHead.setLen_head(values4Short[2]);
                byte[] bytes4Int = new byte[4];

                for(int n = 0; n < this.totalHead.getNum_ivec(); ++n) {
                    MlvModelItem msModelItem = new MlvModelItem();
                    dataInputStream.reset();
                    dataInputStream.skipBytes(8 + n * 16);
                    dataInputStream.readFully(bytes4Int);
                    int k = getInt(bytes4Int);
                    this.ivecTypeUse[k] = 1;
                    msModelItem.setIvec_type(MlvIvecTypeItem.forValue(k));

                    for(int i = 0; i < 2; ++i) {
                        dataInputStream.readFully(bytes4Short);
                        values4Short[i] = getShort(bytes4Short);
                    }

                    msModelItem.setOffset(values4Short[0]);
                    msModelItem.setLen(values4Short[1]);
                    dataInputStream.readFully(bytes4Int);
                    msModelItem.setPost_type(MlvPostTypeItem.forValue(getInt(bytes4Int)));
                    dataInputStream.readByte();
                    byte[] md = new byte[]{dataInputStream.readByte(), dataInputStream.readByte(), dataInputStream.readByte()};
                    msModelItem.setMd5Info(this.bytes2Hex(md));
                    if (isNeedFloat) {
                        dataInputStream.reset();
                        dataInputStream.skipBytes(this.totalHead.getLen_head() + msModelItem.getOffset());
                        byte[] da = new byte[msModelItem.getLen()];
                        dataInputStream.readFully(da);
                        msModelItem.setData(byteToFloatArray(da));
                    }

                    itemMap.put(msModelItem.getIvec_type(), msModelItem);
                }

                this.plp_tv = (MlvModelItem)itemMap.get(MlvIvecTypeItem.plp_tv);
                this.plp_dnn_tv = (MlvModelItem)itemMap.get(MlvIvecTypeItem.plp_dnn_tv);
                this.pncc_tv = (MlvModelItem)itemMap.get(MlvIvecTypeItem.pncc_tv);
                this.pncc_dnn_tv = (MlvModelItem)itemMap.get(MlvIvecTypeItem.pncc_dnn_tv);
                this.cnn = (MlvModelItem)itemMap.get(MlvIvecTypeItem.cnn);
                itemMap.clear();
                dataInputStream.close();
            } catch (Throwable var37) {
                var7 = var37;
                throw var37;
            } finally {
                if (dataInputStream != null) {
                    if (var7 != null) {
                        try {
                            dataInputStream.close();
                        } catch (Throwable var36) {
                            var7.addSuppressed(var36);
                        }
                    } else {
                        dataInputStream.close();
                    }
                }

            }
        } catch (Throwable var39) {
            var5 = var39;
            throw var39;
        } finally {
            if (in != null) {
                if (var5 != null) {
                    try {
                        in.close();
                    } catch (Throwable var35) {
                        var5.addSuppressed(var35);
                    }
                } else {
                    in.close();
                }
            }

        }
    }

    private String bytes2Hex(byte[] md) {
        StringBuffer sb = new StringBuffer();

        for(int i = 0; i < md.length; ++i) {
            String hex = Integer.toHexString(md[i] & 255);
            if (hex.length() < 2) {
                sb.append(0);
            }

            sb.append(hex);
        }

        return sb.toString();
    }

    public int[] getIvecTypeUse() {
        return this.ivecTypeUse;
    }

    public void setIvecTypeUse(int[] ivecTypeUse) {
        this.ivecTypeUse = ivecTypeUse;
    }

    public MlvTotalHead getTotalHead() {
        return this.totalHead;
    }

    public void setTotalHead(MlvTotalHead totalHead) {
        this.totalHead = totalHead;
    }

    public MlvModelItem getPlp_tv() {
        return this.plp_tv;
    }

    public void setPlp_tv(MlvModelItem plp_tv) {
        this.plp_tv = plp_tv;
    }

    public MlvModelItem getPlp_dnn_tv() {
        return this.plp_dnn_tv;
    }

    public void setPlp_dnn_tv(MlvModelItem plp_dnn_tv) {
        this.plp_dnn_tv = plp_dnn_tv;
    }

    public MlvModelItem getPncc_tv() {
        return this.pncc_tv;
    }

    public void setPncc_tv(MlvModelItem pncc_tv) {
        this.pncc_tv = pncc_tv;
    }

    public MlvModelItem getPncc_dnn_tv() {
        return this.pncc_dnn_tv;
    }

    public void setPncc_dnn_tv(MlvModelItem pncc_dnn_tv) {
        this.pncc_dnn_tv = pncc_dnn_tv;
    }

    public MlvModelItem getCnn() {
        return this.cnn;
    }

    public void setCnn(MlvModelItem cnn) {
        this.cnn = cnn;
    }

    static int getInt(byte[] bytes) {
        return 255 & bytes[0] | '\uff00' & bytes[1] << 8 | 16711680 & bytes[2] << 16 | -16777216 & bytes[3] << 24;
    }

    static short getShort(byte[] bytes) {
        return (short)(255 & bytes[0] | '\uff00' & bytes[1] << 8);
    }

    private static float[] byteToFloatArray(byte[] bytes) throws Exception {
        FloatBuffer buf = ByteBuffer.wrap(bytes, 0, bytes.length).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer();
        float[] floatArray = new float[buf.limit()];
        buf.get(floatArray);
        return floatArray;
    }
}
